package com.xggstudio.crm.ui.mvp.v;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xggstudio.crm.R;
import com.xggstudio.crm.base.App;
import com.xggstudio.crm.base.BaseActivity;
import com.xggstudio.crm.base.Constant;
import com.xggstudio.crm.ui.mvp.m.BaseData;
import com.xggstudio.crm.ui.mvp.m.CusInfo;
import com.xggstudio.crm.ui.mvp.p.P_CusInfo;
import com.xggstudio.crm.ui.mvp.v.adapter.MultiItemCommonAdapter;
import com.xggstudio.crm.ui.mvp.v.adapter.MultiItemTypeSupport;
import com.xggstudio.crm.ui.mvp.v.adapter.ViewHolder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class AddEditCusActivity extends BaseActivity implements ICusInfo{

    private Toolbar toolbar;

    private RecyclerView list;

    private MultiItemCommonAdapter adapter;

    private P_CusInfo pCusInfo;

    private CusInfo info;

    private int id;

    @Override
    protected int getLayout() {
        return R.layout.activity_add_or_edit_cus;
    }

    @SuppressLint("RestrictedApi")
    @Override
    protected void init(Bundle savedInstanceState) {

        list = (RecyclerView) $(R.id.list);

        toolbar = (Toolbar) $(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("客户详情");

        initAdapter();
        getIntentData();

        //if(id != 0) {
            pCusInfo = new P_CusInfo(this);
            pCusInfo.getInfo(id);
        //}
    }

    public void getIntentData(){
        if(getIntent() != null){
            id = getIntent().getIntExtra(Constant.CUS_ID,0);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add_cus,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case R.id.menu_add:
                pCusInfo.addCus(info);
                Toast.makeText(this, "jajajaj", Toast.LENGTH_SHORT).show();
                break;
            case android.R.id.home:
                finish();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    private void initAdapter(){

        List<BaseData> data = new ArrayList<>();
        //if(id == 0) {
            for (int i = 0; i < 7; i++) {
                data.add(new BaseData(i));
            }
        //}

        adapter = new MultiItemCommonAdapter<BaseData>(this, data, new MultiItemTypeSupport<BaseData>() {
            @Override
            public int getLayoutId(int itemType) {

                switch (itemType){
                    case 0:
                        return R.layout.view_cus_info_name;
                    case 1:
                        return R.layout.view_cus_info_updata_tag;
                    case 2:
                        return R.layout.view_cus_info_assistant;
                    case 3:
                        return R.layout.view_cus_info_follow_man;
                    case 4:
                        return R.layout.view_cus_info;
                    case 5:
                        return R.layout.view_cus_info_status;
                    case 6:
                        return R.layout.view_cus_callback_log;
                }

                return R.layout.view_cus_info_name;
            }

            @Override
            public int getItemViewType(int position, BaseData baseData) {
                return baseData.getType();
            }
        }) {

            @Override
            public void convert(ViewHolder holder, BaseData baseData, int position) {

                int type = getItemViewType(position);

                switch (type){
                    case 0:
                        nameManger(holder,baseData,position);
                        break;
                    case 1:
                        updatatagManger(holder,baseData,position);
                        break;
                    case 2:
                        assistantManger(holder,baseData,position);
                        break;
                    case 3:
                        followmanManger(holder,baseData,position);
                        break;
                    case 4:
                        infoManger(holder,baseData,position);
                        break;
                    case 5:
                        statusManger(holder,baseData,position);
                        break;
                    case 6:
                        callbacklogManger(holder,baseData,position);
                        break;
                 }

            }

            private void nameManger(ViewHolder holder, BaseData baseData, int position){

                CusInfo temp =  ((CusInfo) baseData.getT());
                if(temp != null){
                    CusInfo.ResultBean.UserBean info = temp.getResult().getUser();
                    List<CusInfo.ResultBean.AdminUserBean> entity = temp.getResult().getAdmin_user();
                    if(entity != null && info != null){
                        for (int i = 0; i < entity.size(); i++) {
                            CusInfo.ResultBean.AdminUserBean b = entity.get(i);
                            if(info.getUid() == b.getId()){
                                holder.setText(R.id.name,b.getName());
                                break;
                            }
                        }
                    }

                }

            }

            private void updatatagManger(ViewHolder holder, BaseData baseData, int position){

                LinearLayout layout = holder.getView(R.id.layout_content);

                CusInfo temp =  ((CusInfo) baseData.getT());
                if(temp != null){
                    List<CusInfo.ResultBean.CrmFollowsListBean> entity = temp.getResult().getCrm_follows_list();
                    if(entity != null){
                        for (int i = 0; i < entity.size(); i++) {

                            CusInfo.ResultBean.CrmFollowsListBean info = entity.get(i);

                            View view = View.inflate(mContext,R.layout.view_cus_updata_item,null);
                            TextView name = view.findViewById(R.id.name);
                            TextView time = view.findViewById(R.id.time);
                            TextView content = view.findViewById(R.id.content);

                            name.setText(info.getAdmin_name());
                            time.setText(info.getUpdated_at());
                            content.setText(info.getContent());

                            layout.addView(view);

                        }
                    }
                }

            }

            private void assistantManger(ViewHolder holder, BaseData baseData, int position){

                final CusInfo temp =  ((CusInfo) baseData.getT());

                LinearLayout layout = holder.getView(R.id.layout_content);

                if(temp != null){

                    layout.removeAllViews();

                    List<CusInfo.ResultBean.AssistBean> entity = temp.getResult().getAssist();
                    if(entity != null){
                        for (int i = 0; i < entity.size(); i++) {

                            CusInfo.ResultBean.AssistBean b = entity.get(i);

                            View view = View.inflate(mContext,R.layout.view_cus_assist_item,null);
                            TextView name = view.findViewById(R.id.name);
                            TextView ratio = view.findViewById(R.id.ratio);

                            name.setText(getUserName(b.getUser_id()));
                            ratio.setText(b.getPercent()+"");

                            layout.addView(view);

                        }
                    }

                    Log.e("AddEditCusActivity", "assistantManger: " + temp.getResult().getAssist().size());
                }

                holder.setOnClickListener(R.id.ass_name, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"跟单人");
                        b.putSerializable(Constant.CUS_CONTENT, (Serializable) temp.getResult().getAdmin_user());
                        b.putInt(Constant.CUS_RESULS,2);
                        startToActivity(AddAssistantActivity.class,b,2);

                    }
                });

            }

            private void followmanManger(final ViewHolder holder, BaseData baseData, int position){

                CusInfo temp =  ((CusInfo) baseData.getT());

                if(temp != null){
                    CusInfo.ResultBean.UserBean info = temp.getResult().getUser();
                    if(info != null){
                        holder.setText(R.id.ass_name,getString(info.getClient_documentary()));

                    }

                }

                holder.setOnClickListener(R.id.ass_name, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String text = holder.getText(R.id.ass_name).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"跟单人");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,3);
                        startToActivity(EditActivity.class,b,3);
                    }
                });

            }

            private void infoManger(final ViewHolder holder, BaseData baseData, int position){


                CusInfo info = ((CusInfo) baseData.getT());
                if(info != null) {
                    CusInfo.ResultBean.UserBean temp = info.getResult().getUser();

                    if (temp != null) {

                        holder.setText(R.id.name, getString(temp.getClient_name()));
                        holder.setText(R.id.phone, getString(temp.getClient_phone()));
                        holder.setText(R.id.wechat, getString(temp.getClient_wechat()));
                        holder.setText(R.id.qq, getString(temp.getClient_qq()));
                        holder.setText(R.id.email, getString(temp.getClient_email()));
                        holder.setText(R.id.sex, temp.getClient_sex() == 1 ? "女" : "男");
                        holder.setText(R.id.come, temp.getClient_online() == 1?"线上":"线下");
                        holder.setText(R.id.year, getString(temp.getClient_age()));
                        holder.setText(R.id.company, getString(temp.getClient_company()));
                        holder.setText(R.id.region, temp.getClient_province()+"-"+temp.getClient_city() + "-" + temp.getClient_county());
                        holder.setText(R.id.address, getString(temp.getClient_address()));
                    }
                }

                holder.setOnClickListener(R.id.name, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String text = holder.getText(R.id.name).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"姓名");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,4);
                        startToActivity(EditActivity.class,b,4);
                    }
                });

                holder.setOnClickListener(R.id.phone, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String text = holder.getText(R.id.phone).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"电话");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,4);
                        startToActivity(EditActivity.class,b,4);
                    }
                });

                holder.setOnClickListener(R.id.wechat, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String text = holder.getText(R.id.wechat);


                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"微信");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,4);
                        startToActivity(EditActivity.class,b,4);
                    }
                });

                holder.setOnClickListener(R.id.qq, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String text = holder.getText(R.id.qq).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"QQ");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,4);
                        startToActivity(EditActivity.class,b,4);
                    }
                });

                holder.setOnClickListener(R.id.email, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String text = holder.getText(R.id.email).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"E-mail");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,4);
                        startToActivity(EditActivity.class,b,4);
                    }
                });

                holder.setOnClickListener(R.id.sex, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String text = holder.getText(R.id.sex).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"性别");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,4);
                        startToActivity(EditActivity.class,b,4);
                    }
                });

                holder.setOnClickListener(R.id.come, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String text = holder.getText(R.id.come).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"客户来源");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,4);
                        startToActivity(EditActivity.class,b,4);
                    }
                });

                holder.setOnClickListener(R.id.year, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String text = holder.getText(R.id.year).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"年龄");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,4);
                        startToActivity(EditActivity.class,b,4);
                    }
                });

                holder.setOnClickListener(R.id.company, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String text = holder.getText(R.id.company).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"公司");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,4);
                        startToActivity(EditActivity.class,b,4);
                    }
                });

                holder.setOnClickListener(R.id.region, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String text = holder.getText(R.id.region).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"地区");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,4);
                        startToActivity(EditActivity.class,b,4);
                    }
                });

                holder.setOnClickListener(R.id.address, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        String text = holder.getText(R.id.address).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"地址");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,4);
                        startToActivity(EditActivity.class,b,4);
                    }
                });
            }

            private void statusManger(final ViewHolder holder, BaseData baseData, int position){

                CusInfo info = ((CusInfo) baseData.getT());
                if(info != null) {
                    CusInfo.ResultBean.UserBean temp = info.getResult().getUser();

                    if (temp != null) {

                        // 客户类型 1留学2移民3房产4龟龟伙伴
                        holder.setText(R.id.type, getCusType(temp.getClass_id()));
                        holder.setText(R.id.stutas, temp.getClient_status() == 2 ? "无需跟进" : "跟进中");
                        holder.setText(R.id.time, temp.getClient_next_visit());

                    }
                }

                holder.setOnClickListener(R.id.type, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String text = holder.getText(R.id.type).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"客户类型");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,5);
                        startToActivity(EditActivity.class,b,5);
                    }
                });

                holder.setOnClickListener(R.id.stutas, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String text = holder.getText(R.id.stutas).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"状态");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,5);
                        startToActivity(EditActivity.class,b,5);
                    }
                });

                holder.setOnClickListener(R.id.time, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String text = holder.getText(R.id.time).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"下次回访");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,5);
                        startToActivity(EditActivity.class,b,5);
                    }
                });

            }

            private void callbacklogManger(final ViewHolder holder, BaseData baseData, int position){

                CusInfo info = ((CusInfo) baseData.getT());
                if(info != null) {
                    CusInfo.ResultBean.UserBean temp = info.getResult().getUser();

                    if (temp != null) {
                        //holder.setText(R.id.content,)
                    }
                }

                holder.setOnClickListener(R.id.content, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String text = holder.getText(R.id.content).toString();

                        Bundle b = new Bundle();
                        b.putString(Constant.CUS_NAME,"回访内容");
                        b.putString(Constant.CUS_CONTENT,text);
                        b.putInt(Constant.CUS_RESULS,6);
                        startToActivity(EditActivity.class,b,6);
                    }
                });

            }

            private String getCusType(int type){
                switch (type){
                    case 1:
                        return "留学";
                    case 2:
                        return "移民";
                    case 3:
                        return "房产";
                    case 4:
                        return "龟龟伙伴";
                    default:
                        return "留学";
                }

            }
        };

        LinearLayoutManager LayoutManager = new LinearLayoutManager(this);
        LayoutManager.setAutoMeasureEnabled(true);
        list.setLayoutManager(LayoutManager);
        list.setAdapter(adapter);

        //info = new CusInfo();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(RESULT_CANCELED == resultCode){
            return;
        }

        switch (resultCode){
            case 0:
                adminMangerData(data);
                break;
            case 1:

                break;
            case 2:
                assistantManger(data);
                break;
            case 3:
                followmanMager(data);
                break;
            case 4:
                infoMangerData(data);
                break;
            case 5:
                statusManger(data);
                break;
            case 6:

                break;
        }

        List<BaseData> arrays = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            arrays.add(new BaseData(i,info));
        }

        adapter.setmDatas(arrays);

    }

    private void adminMangerData(Intent data){


    }

    private void assistantManger(Intent data){

        if(info != null){
            CusInfo.ResultBean.AdminUserBean temp = (CusInfo.ResultBean.AdminUserBean) data.getSerializableExtra(Constant.CUS_CONTENT);

            List<CusInfo.ResultBean.AssistBean>  entity = info.getResult().getAssist();

            CusInfo.ResultBean.AssistBean b = new CusInfo.ResultBean.AssistBean();
            b.setPercent(temp.getRatio());
            b.setUser_id(getUserid(temp.getName()));

            entity.add(b);

            info.getResult().setAssist(entity);

        }

    }

    private void followmanMager(Intent data){
        if(info != null){

            String title = data.getStringExtra(Constant.CUS_NAME);
            String content = data.getStringExtra(Constant.CUS_CONTENT);

            info.getResult().getUser().setClient_documentary(content);

        }
    }

    // 处理客户信息
    private void infoMangerData(Intent data){

        if(info != null){

            String title = data.getStringExtra(Constant.CUS_NAME);
            String content = data.getStringExtra(Constant.CUS_CONTENT);
            switch (title) {
                case "姓名":
                    info.getResult().getUser().setClient_name(content);
                    break;
                case "电话":
                    info.getResult().getUser().setClient_phone(content);
                    break;
                case "微信":
                    info.getResult().getUser().setClient_wechat(content);
                    break;
                case "QQ":
                    info.getResult().getUser().setClient_qq(content);
                    break;
                case "E-mail":
                    info.getResult().getUser().setClient_email(content);
                    break;
                case "性别":
                    info.getResult().getUser().setClient_sex(content.equals("女")?1:2);
                    break;
                case "客户来源":
                    info.getResult().getUser().setClient_online(content.equals("线上")?1:2);
                    break;
                case "年龄":
                    info.getResult().getUser().setClient_age(content);
                    break;
                case "公司":
                    info.getResult().getUser().setClient_company(content);
                    break;
                case "地区":

                    Log.e("AddEditCusActivity", "infoMangerData: " + content) ;

                    String[] array = content.split("-");

                    String province = array[0];
                    String city = array[1];
                    String county = array[2];

                    info.getResult().getUser().setClient_province(province);
                    info.getResult().getUser().setClient_city(city);
                    info.getResult().getUser().setClient_county(county);
                    break;
                case "地址":
                    info.getResult().getUser().setClient_address(content);
                    break;
            }
        }
    }

    private void statusManger(Intent data){

        if(info != null){

            String title = data.getStringExtra(Constant.CUS_NAME);
            String content = data.getStringExtra(Constant.CUS_CONTENT);

            switch (title) {
                case "客户类型":
                    info.getResult().getUser().setClass_id(getTypeID(content));
                    break;
                case "状态":
                    info.getResult().getUser().setClient_status(getStatusId(content));
                    break;
                case "下次回访":
                    info.getResult().getUser().setClient_next_visit(content);
                    break;
            }

        }
    }

    @Override
    public void showInfo(CusInfo data) {



        if(id == 0){
            CusInfo.ResultBean.UserBean userBean = new CusInfo.ResultBean.UserBean();
            List<CusInfo.ResultBean.AssistBean> assistBeans = new ArrayList<>();
            List<CusInfo.ResultBean.CrmFollowsListBean> crmFollowsListBeans = new ArrayList<>();

            int uid = App.getInstance().getAssistList().getResult().get(0).getId();

            userBean.setUid(uid);
            userBean.setClient_sex(1);
            userBean.setClient_online(1);
            userBean.setClient_status(1);
            userBean.setClass_id(1);


            data.getResult().setUser(userBean);
            data.getResult().setAssist(assistBeans);
            data.getResult().setCrm_follows_list(crmFollowsListBeans);
        }

        List<BaseData> arrays = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            arrays.add(new BaseData(i,data));
        }

        adapter.setmDatas(arrays);

        this.info = data;
    }

    private int getTypeID(String name){
        switch (name){
            case "留学":
                return 1;
            case "移民":
                return 2;
            case "房产":
                return 3;
            case "龟龟伙伴":
                return 4;
            default:return 0;
        }
    }

    private int getStatusId(String name){

        switch (name) {
            case "跟进中":
                return 1;
            case "无需跟进":
                return 2;
            default:return 0;
        }

    }

    private String getUserName(int id){
        if(info != null){
            for (int i = 0; i < info.getResult().getAdmin_user().size(); i++) {

                CusInfo.ResultBean.AdminUserBean temp = info.getResult().getAdmin_user().get(i);

                if(temp.getId() == id){
                    return temp.getName();
                }

            }
        }

        return "";
    }

    private int getUserid(String name){
        if(info != null){
            for (int i = 0; i < info.getResult().getAdmin_user().size(); i++) {

                CusInfo.ResultBean.AdminUserBean temp = info.getResult().getAdmin_user().get(i);

                if(temp.getName().equals(name)){
                    return temp.getId();
                }

            }
        }

        return 0;
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void noNet() {

    }

    @Override
    public void addsucceed() {

    }

    @Override
    public void editsucceed() {

    }

    public String getString(String s){
        if(s == null){

            return "";
        }

        return s;
    }

}
